//
//  ApplyDDViewController.m
//  HuiKaoClient
//
//  Created by Wang Li on 13-3-8.
//  Copyright (c) 2013年 龚 杰. All rights reserved.
//

#import "ApplyDDViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "ASIHTTPRequest.h"
#import "CJSONDeserializer.h"
#import "AppDelegate.h"
#import "LoginViewController.h"
#import "MainTabBarController.h"
#import "PayWebViewController.h"
@interface ApplyDDViewController ()

@end

@implementation ApplyDDViewController
@synthesize m_ProdIDs;
@synthesize m_ProdNums;
@synthesize m_ProPrice;
@synthesize m_Name;
@synthesize m_Address;
@synthesize m_Phone;
@synthesize m_Shipping;
- (id)init
{
    if (self) {
        // Custom initialization
    }
    return self;
}

-(void) dealloc
{
    [_OrderDic release];
     [m_ProdIDs release];
     [m_ProdNums release];
     [m_ProPrice release];
     [m_Name release];
     [m_Address release];
     [m_Phone release];
     [m_Shipping release];
    [m_titleBttBarItem release];
    [super dealloc];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
//    UILabel* tNavTitleLab = (UILabel*)[self.navigationController.navigationBar viewWithTag:144];
//    [tNavTitleLab setText:@"订单提交"];
    
    m_titleBttBarItem = [[MainTabBarController setTitle:@"订单提交"] retain];
    _NavBtt = [UIButton buttonWithType:UIButtonTypeCustom];
    [_NavBtt setImage:[UIImage imageNamed:@"nav_back.png"] forState:UIControlStateNormal];
    [_NavBtt addTarget:self action:@selector(navPopPressed) forControlEvents:UIControlEventTouchUpInside];
    _NavBtt.frame =CGRectMake(4, 7, 50, 30);
    [_NavBtt retain];
    UILabel* tBackLab = [[UILabel alloc] initWithFrame:CGRectMake(14, 5, 30, 20)];
    [tBackLab setBackgroundColor:[UIColor clearColor]];
    [tBackLab setTextColor:[UIColor whiteColor]];
    [tBackLab setText:@"返回"];
    [tBackLab setFont:[UIFont systemFontOfSize:13]];
    [_NavBtt addSubview:tBackLab];
    [tBackLab release];
    
    
    [self requestDetailData];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void) viewWillAppear:(BOOL)animated
{
    [self.navigationController.navigationBar addSubview:_NavBtt];
    [self.navigationController.navigationBar addSubview:m_titleBttBarItem];
}
-(void) viewWillDisappear:(BOOL)animated
{
    [_NavBtt removeFromSuperview];
    [m_titleBttBarItem removeFromSuperview];
}

-(void) navPopPressed
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void) cofirmDD
{
    PayWebViewController* tPayWebVC = [[PayWebViewController alloc] init];
    tPayWebVC.navigationItem.hidesBackButton =YES;
    tPayWebVC.m_OderIDStr = [NSString stringWithFormat:@"%d",[[_OrderDic objectForKey:@"order_id"] intValue]];
    [self.navigationController pushViewController:tPayWebVC animated:YES];
    [tPayWebVC release];
}


-(void) requestAddChartFinish:(ASIHTTPRequest*)request
{
    NSError* tError;
    NSString* tReqStr = [request responseString];
    NSLog(@"ads json data =%@",tReqStr);
    NSData *jsonData = [tReqStr dataUsingEncoding:NSUTF32BigEndianStringEncoding];
	NSDictionary* tDataDic = [[CJSONDeserializer deserializer] deserializeAsDictionary:jsonData error:&tError];
    AppDelegate* tAppDel = (AppDelegate*)[UIApplication sharedApplication].delegate;
    if (tDataDic != nil) {
        int tErrorCode =[[tDataDic objectForKey:@"error_code"] intValue];
        if (tErrorCode == 0) {
            [_OrderDic release];
            _OrderDic = nil;
            _OrderDic = [tDataDic objectForKey:@"data"];
            [_OrderDic retain];
            
            UIView* tDDView = [[UIView alloc] initWithFrame:CGRectMake(10, 12, 300, 120)];
            [tDDView setBackgroundColor:[UIColor colorWithRed:252/255.f green:243/255.f blue:226/255.f alpha:1]];
            tDDView.layer.borderWidth =1;
            tDDView.layer.borderColor = [UIColor colorWithRed:232/255.f green:194/255.f blue:133/255.f alpha:1].CGColor;
            tDDView.layer.cornerRadius = 8.f;
            [self.view addSubview:tDDView];
            [tDDView release];
            
            UILabel* tSusLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 18, 300, 30)];
            [tSusLab setBackgroundColor:[UIColor clearColor]];
            [tSusLab setFont:[UIFont systemFontOfSize:20]];
            [tSusLab setText:@"订单提交成功"];
            tSusLab.textAlignment = UITextAlignmentCenter;
            [tSusLab setTextColor:[UIColor colorWithRed:251/255.f green:153/255.f blue:0 alpha:1]];
            [tDDView addSubview:tSusLab];
            [tSusLab release];
            
            UILabel* tDesLab = [[UILabel alloc] initWithFrame:CGRectMake(10, 60, 280, 40)];
            tDesLab.numberOfLines =2;
            [tDesLab setBackgroundColor:[UIColor clearColor]];
            [tDesLab setFont:[UIFont systemFontOfSize:12]];
            [tDesLab setText:@"订单提交成功，请尽快支付，如有任何问题请通过客服电话400-6666-798与我们取得联系。"];
            [tDDView addSubview:tDesLab];
            [tDesLab release];
            
            UIButton* tApplyBtt = [UIButton buttonWithType:UIButtonTypeCustom];
            tApplyBtt.frame =CGRectMake(10, 146, 300, 40);
            [tApplyBtt setImage:[UIImage imageNamed:@"button_yellow_l.png"] forState:UIControlStateNormal];
            [self.view addSubview:tApplyBtt];
            [tApplyBtt addTarget:self action:@selector(cofirmDD) forControlEvents:UIControlEventTouchUpInside];
            UILabel* tBttlab = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, 300, 20)];
            [tBttlab setBackgroundColor:[UIColor clearColor]];
            [tBttlab setTextColor:[UIColor whiteColor]];
            [tBttlab setText:@"在线支付"];
            tBttlab.textAlignment =UITextAlignmentCenter;
            [tBttlab setFont:[UIFont boldSystemFontOfSize:17]];
            [tApplyBtt addSubview:tBttlab];
            [tBttlab release];
        }else{
            if ([tDataDic objectForKey:@"error_msg"] != nil) {
                [tAppDel showWithCustomView:[tDataDic objectForKey:@"error_msg"] detailText:@"" isCue:-1 delayTime:2];
            }else{
                [tAppDel showWithCustomView:@"数据错误" detailText:@"" isCue:YES delayTime:2];
            }
            
        }
    }
    [self hudWasHidden:nil];
}




-(void) requestDetailData
{
    
    MainTabBarController* tMainTabController = (MainTabBarController*)self.navigationController.tabBarController;
    
    [self addHudLoad];
    NSDictionary* tDataDic = [tMainTabController.m_UserDic objectForKey:@"data"];
    NSString* tURLStr = nil;
    if ( tMainTabController.m_UserDic != nil) {
        tURLStr = [NSString stringWithFormat:@"%@ac=order&product_ids=%@&product_numbers=%@&cur_user_id=%@&name=%@&address=%@&mobile=%@&shipping=%@",RequestCurrentURL,m_ProdIDs,m_ProdNums,[tDataDic objectForKey:@"user_id"],m_Name,m_Address,m_Phone,m_Shipping];
    }else{
        tURLStr = [NSString stringWithFormat:@"%@ac=order&product_ids=%@&product_numbers=%@&name=%@&address=%@&mobile=%@&shipping=%@",RequestCurrentURL,m_ProdIDs,m_ProdNums,m_Name,m_Address,m_Phone,m_Shipping];
    }
     tURLStr = (NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)tURLStr, nil, nil, kCFStringEncodingUTF8);
    ASIHTTPRequest* tRequestPage = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:tURLStr]];
    [tRequestPage setDelegate:self];
    [tRequestPage setDidFailSelector:@selector(requestDetailFail:)];
    [tRequestPage setDidFinishSelector:@selector(requestAddChartFinish:)];
    [tRequestPage startAsynchronous];
    
}

-(void) requestDetailFail:(ASIHTTPRequest*)request
{
    AppDelegate* tAppDel = (AppDelegate*)[UIApplication sharedApplication].delegate;
    [tAppDel showWithCustomView:@"网络错误" detailText:@"" isCue:YES delayTime:2];
    [self hudWasHidden:nil];
}

#pragma mark -
#pragma mark MBProgressHUDDelegate
- (void)hudWasHidden:(MBProgressHUD *)hud{
	[_MBHUD removeFromSuperview];
    _MBHUD.delegate = nil;
	[_MBHUD release];
	_MBHUD=nil;
}

-(void) addHudLoad{
	if (_MBHUD == nil) {
		_MBHUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
		// Add HUD to screen
		[self.navigationController.view addSubview:_MBHUD];
		// Regisete for HUD callbacks so we can remove it from the window at the right time
		_MBHUD.delegate = self;
		_MBHUD.labelText = @"请求数据中...";
		[_MBHUD show:YES];
	}
}

@end
